package com.zxyankh.raft;

import com.zxyankh.raft.context.ServerContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

import java.util.concurrent.TimeUnit;

/**
 * Description: 启动类
 *
 * @author ankh
 * @since 06.09.2017
 */
@SpringBootApplication
@EnableAutoConfiguration
public class Starter {

    /**
     * Description: 启动参数列表:
     * -configPath= 配置文件路径
     *
     * @param args 启动参数
     */
    public static void main(String[] args) {
        ServerContext.init();
        SpringApplication.run(Starter.class, args);
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        // 指定tomcat端口
        factory.setPort(ServerContext.getCurrentServerIpPort().getHttpPort());
        factory.setSessionTimeout(10, TimeUnit.MINUTES);
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
        return factory;
    }
}
